package edu.gatech.teamrocket;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import edu.gatech.teamrocket.models.*;

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

	private static final String DATABASE_NAME = "dew.db";
	private static final int DATABASE_VERSION = 5;

	private Dao<User, String> userDao;
	private Dao<Task, Integer> taskDao;
	private Dao<Category, Integer> categoryDao;

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase sqliteDatabase, ConnectionSource connectionSource) {
		try {
			TableUtils.createTable(connectionSource, User.class);
			TableUtils.createTable(connectionSource, Task.class);
			TableUtils.createTable(connectionSource, Category.class);
			Dao<User, String> userDao = this.getUserDao();
			Dao<Category, Integer> categoryDao = this.getCategoryDao();
			User defaultUser = new User("default", "default");
			userDao.create(defaultUser); // create a default user
			// create the default categories
			categoryDao.create(new Category("Personal", defaultUser));
			categoryDao.create(new Category("School", defaultUser));
			categoryDao.create(new Category("Work", defaultUser));
		} catch (SQLException e) {
			Log.e(DatabaseHelper.class.getName(), "Unable to create database.", e);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase sqliteDatabase, ConnectionSource connectionSource, int oldVer, int newVer) {
		try {
			TableUtils.dropTable(connectionSource, User.class, true);
			TableUtils.dropTable(connectionSource, Task.class, true);
			TableUtils.dropTable(connectionSource, Category.class, true);
			onCreate(sqliteDatabase, connectionSource);
		} catch (SQLException e) {
			Log.e(DatabaseHelper.class.getName(), "Unable to upgrade database from version " + oldVer + " to new "
					+ newVer, e);
		}
	}
	
	public void clearTasks() {
		try {
			TableUtils.clearTable(connectionSource, Task.class);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void clearUsers() {
		try {
			TableUtils.clearTable(connectionSource, User.class);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Dao<User, String> getUserDao() throws SQLException {
		if (userDao == null) {
			userDao = getDao(User.class);
		}
		return userDao;
	}
	
	public Dao<Task, Integer> getTaskDao() throws SQLException {
		if (taskDao == null) {
			taskDao = getDao(Task.class);
		}
		
		return taskDao;
	}
	
	public Dao<Category, Integer> getCategoryDao() throws SQLException {
		if (categoryDao == null) {
			categoryDao = getDao(Category.class);
		}
		
		return categoryDao;
	}
}
